
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 3
 * @Author : WangRuoyu 
 * @Date : 2023/5/10 19:27
 */

#include <vector>
#include "iostream"
#include "cstring"
#include "algorithm"

using namespace std;

bool check(string s) {
    if (s.size() == 2 && s[0] == '-' && s[1] >= 'a' && s[1] <= 'z') {
        return true;
    }
    return false;
}

int main() {
    string str;
    cin >> str;
    int op[26] = {0};
    for (int i = 0; i < str.size(); ++i) {
        if (i + 1 < str.size() && str[i + 1] == ':') {
            op[str[i] - 'a'] = 1;
            i++;
        } else {
            op[str[i] - 'a'] = -1;
        }
    }

    int n;
    cin >> n;
    getchar();
    for (int i = 1; i <= n; ++i) {
        cout << "Case " << i << ":";
        getline(cin, str);
        vector<string> strs;
        string tmp;
        for (int j = 0; j < str.size(); ++j) {
            if (str[j] == ' ') {
                if (!tmp.empty()) {
                    strs.push_back(tmp);
                    tmp = "";
                }
            } else {
                tmp += str[j];
            }
        }
        if (!tmp.empty()) {
            strs.push_back(tmp);
        }

        bool st[26] = {false};
        string num[26];

        for (int j = 1; j < strs.size(); ++j) {
            string ss = strs[j];
            int idx = ss[1] - 'a';
            if (check(ss)) {
                if (op[idx] == 1) {
                    if (j + 1 < strs.size()) {
                        st[idx] = true;
                        num[idx] = strs[j + 1];
                        j++;
                    } else {
                        break;
                    }
                } else if (op[idx] == -1) {
                    st[idx] = true;
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        for (int j = 0; j < 26; ++j) {
            if (st[j]) {
                if (op[j] == 1) {
                    cout << " -" << char(j + 'a') << " " + num[j];
                } else if (op[j] == -1) {
                    cout << " -" << char(j + 'a');
                }
            }
        }
        cout << endl;
    }

    return 0;
}